package com.chujian.system.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.chujian.framework.component.def.YForm;
import com.chujian.framework.component.def.YQuery;
import com.chujian.framework.component.enums.ComponentType;
import com.chujian.framework.core.base.BaseServiceImpl;
import com.chujian.core.entity.CoreComponent;
import com.chujian.system.core.mapper.CoreComponentMapper;
import com.chujian.system.core.service.CoreComponentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


/**
 * 组件 服务实现类
 *
 * @author yj
 * @since 2021-08-25
 */
@Service
@Slf4j
public class CoreComponentServiceImpl extends BaseServiceImpl<CoreComponentMapper, CoreComponent> implements CoreComponentService {

    @Override
    public CoreComponent saveQueryComponent(YQuery query) {
        CoreComponent component = new CoreComponent()
                .setComponentId(query.getId())
                .setComponentName(query.getName())
                .setComponentType(ComponentType.QUERY_COMPONENT.getId())
                .setCfg(JSON.toJSONString(query));
        return this.saveComponent(component);
    }

    @Override
    public CoreComponent saveFormComponent(YForm form) {
        CoreComponent component = new CoreComponent()
                .setComponentId(form.getId())
                .setComponentName(form.getName())
                .setComponentType(ComponentType.FORM_COMPONENT.getId())
                .setCfg(JSON.toJSONString(form));
        return this.saveComponent(component);
    }

    private CoreComponent saveComponent(CoreComponent component) {
        if (null == component.getComponentId()) {
            this.save(component);
        } else {
            this.updateById(component);
        }
        return component;
    }

}
